package org.pine.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.pine.api.beans.request.PageRequest;
import org.pine.api.beans.request.UserQueryRequest;
import org.pine.api.beans.response.PageResponse;
import org.pine.api.beans.response.UserQueryResponse;
import org.pine.manager.UserManager;
import org.pine.manager.bo.UserBo;
import org.pine.manager.convertor.UserConvertor;
import org.pine.manager.query.UserQuery;
import org.pine.service.UserService;
import org.pine.service.utils.PageUtil;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户服务实现
 *
 * @author pine
 * @version v1.0
 * @since 2025-04-12 16:11
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserManager userManager;
    @Resource
    private UserConvertor userConvertor;

    @Override
    public PageResponse<UserQueryResponse> listByPage(PageRequest<UserQueryRequest> request) {
        // 参数转换
        UserQuery userQuery = userConvertor.queryRequestToQuery(request.getData());
        PageRequest<UserQuery> pageRequest = PageUtil.convertQuery(request, userQuery);

        // 查库
        IPage<UserBo> userBoPage = userManager.listPageBos(pageRequest);
        List<UserBo> userBos = userBoPage.getRecords();

        // 处理响应
        if (CollUtil.isNotEmpty(userBos)) {
            List<UserQueryResponse> list = userBos.stream().map(userConvertor::entityBoToQueryResponse).toList();
            return PageUtil.convertPage(userBoPage, list);
        }
        return new PageResponse<>();
    }
}
